package com.gigi.service.manual;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gigi.entity.CollectShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.mapper.generated.CollectShiqinMapper;
import com.gigi.mapper.manual.CollectManualMapper;
import com.gigi.model.CollectDO;
import com.gigi.model.CollectSaveDO;
import com.gigi.model.ListCollectRequest;
import com.gigi.service.generated.BlogShiqinService;
import com.gigi.service.generated.CollectShiqinService;
import com.gigi.service.generated.TypeShiqinService;
import com.gigi.service.generated.UserShiqinService;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CollectManageService {

    private final CollectShiqinMapper collectShiqinMapper;
    private final CollectShiqinService collectShiqinService;
    private final BlogShiqinService blogShiqinService;
    private final UserShiqinService userShiqinService;
    private final BlogManageService blogManageService;
    private final CollectManualMapper collectManualMapper;
    private final TypeShiqinService typeShiqinService;

    public CollectManageService(CollectShiqinMapper collectShiqinMapper, CollectShiqinService collectShiqinService,
                                BlogShiqinService blogShiqinService, UserShiqinService userShiqinService,
                                BlogManageService blogManageService, CollectManualMapper collectManualMapper,
                                TypeShiqinService typeShiqinService) {
        this.collectShiqinMapper = collectShiqinMapper;
        this.collectShiqinService = collectShiqinService;
        this.blogShiqinService = blogShiqinService;
        this.userShiqinService = userShiqinService;
        this.blogManageService = blogManageService;
        this.collectManualMapper = collectManualMapper;
        this.typeShiqinService = typeShiqinService;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<CollectDO> list(ListCollectRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }
        int startIndex = (request.getPageNum() - 1) * request.getPageSize();

        List<CollectDO> list = collectManualMapper.list(request.getFavoriteId(), request.getUserId(), startIndex, request.getPageSize());
        if (CollUtil.isEmpty(list)) {
            return MultiResponse.of(Collections.emptyList(), 0);
        }

        int total = 0;

        // 传收藏夹id=》分页， 传用户id=》查所有
        if (Objects.nonNull(request.getFavoriteId())) {
            list.forEach(e -> {
                LambdaQueryWrapper<CollectShiqinEntity> wrapper = new LambdaQueryWrapper<CollectShiqinEntity>()
                        .eq(CollectShiqinEntity::getBlogId, e.getId());

                List<String> tagNames = blogManageService.getTagNames(e.getTagNamesStr());
                e.setTime(e.getTimeDate().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
                e.setTagId(tagNames);
                e.setTagNamesStr(StrUtil.join(StrUtil.COMMA, tagNames));
                e.setTypeName(typeShiqinService.getById(e.getTypeId()).getName());
                e.setUserName(userShiqinService.getById(e.getUserId()).getName());
                e.setCollectCount(collectShiqinService.count(wrapper));
            });
            total = collectManualMapper.listCount(request.getFavoriteId(), request.getUserId());
        }
        return MultiResponse.of(list, total);
    }

    /**
     * 新增/删除接口
     *
     * @param request
     * @return
     */
    public Response change(CollectSaveDO request) {
        if (Objects.isNull(request.getId())) {

            List<CollectShiqinEntity> targets = request.getFavoriteId().stream().map(e -> {
                CollectShiqinEntity entity = new CollectShiqinEntity();
                entity.setUserId(request.getUserId());
                entity.setFavoriteId(e);
                entity.setBlogId(request.getBlogId());
                return entity;
            }).collect(Collectors.toList());

            if (!collectShiqinService.saveBatch(targets)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            if (!collectShiqinService.removeByIds(request.getId())) {
                return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

}
